<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\first\model\Notice;
use app\first\controller\Payment;
use think\Db;

class ShopTransfers extends Base {
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
//        p($where);
//        p($field);die;
        $list = $this
            ->field($field,$out)
            ->with(['member','shop'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $pic = new Pic();
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => &$v){
            $v['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $v['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    /**
     * @param int $id
     * @param int $type
     * @param string $info
     * @param null $out
     * @return bool
     * @throws \Yansongda\Pay\Exceptions\GatewayException
     * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     */
    public function confirm($id=0,$type=1,$info='',&$out=null){
        $_info = $this->find($id);
        $user = (new Member())->find($_info['uid']);
        $shop = (new Shop())->find($_info['shop_id']);
        if(!$_info){
            $out = '提现不存在';
            return false;
        }
        if($_info['status']!=1){
            $out = '提现已处理';
            return false;
        }
        if(!$user){
            $out = '用户不存在';
            return false;
        }
        $transfers_id = '';
        $msg = "商铺{$shop['title']}";
        if($type==2){
            if(!$_info){
                $out = '提现拒绝理由';
                return false;
            }
            $_info->info = $info;
            $_info->status = 3;
            $msg .= '提现失败';
        }else{
            if($_info['type']==1){
                $transfers_id = $user['openid'];
            }else{
                $transfers_id = $user['seller_id'];
            }
            $result = (new Payment())
                ->transfers($_info['type'],$transfers_id,$_info['amount'],$_info['title'],build_order_no(),'提现');
            $data = $result->all();
            if($data['result_code']=='SUCCESS'){
                $_info->status = 2;
                $msg .= '提现成功';
            }else{
                $_info->info = '请求提现失败';
                $_info->status = 3;
                $msg .= '提现失败';
            }
        }
        $_info->check_time = time();
        $shop->money = $shop['money']-$_info['amount'];
        Db::startTrans();
        $result = $_info->save();
        $result1 = $shop->save();
        $result2 = (new Notice())->addNew($shop['uid'],'提现操作',$msg,8,'',$id,'');
        if($result && $result1 && $result2){
            Db::commit();
            $out = [
                'title'=> '提现操作',
                'uid'=> $shop['uid'],
                'order_id'=> $transfers_id,
                'id'=> $_info['id'],
                'msg'=>$msg
            ];
            return true;
        }else{
            Db::rollback();
            $out = '提现失败';
            return false;
        }
    }

    /**
     * @param int $id
     * @param string $field
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field('update_time',true)
            ->with(['member','shop'])
            ->where('id','eq',$id)
            ->find();
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function member(){
        return $this->hasOne('Member','id','uid')
            ->bind([
                'account',
                'nickname'
            ]);
    }

    public function shop(){
        return $this->hasOne('Shop','id','shop_id')
            ->bind([
                'shop_name'=>'title',
                'real_name',
                'contact',
                'service'
            ]);
    }

    /**
     * @param $value
     * @return string
     */
    public function setContentAttr($value){
        if($value){
            return htmlspecialchars($value);
        }else{
            return '';
        }
    }

    /**
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        if($value){
            return htmlspecialchars_decode($value);
        }else{
            return '';
        }
    }
}